package com.cnswhy.cloud.server.app.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@Slf4j
public class JsApiPay {
	

	//支付金额
	public int total_fee = 0;
	//用于获取收货地址js函数入口参数
	public String access_token="";
	//用户微信标识,用于调用统一下单接口
//	public String openid="";
	//统一下单接口返回结果
	public static WxPayData unifiedOrderResult;
	
	public String openId;
	
	public String mhId="";
	
	private String appId;
	
	private String notifyUrl;
	
	private String key;
	
	private String scene_info;
	
	private String trade_type;
	
    public JsApiPay(String openid, int total_fee, String mhId, String appId, String notifyUrl, String key, String scene_info, String trade_type)
    {
        this.total_fee = total_fee;
        this.openId = openid;
        this.mhId = mhId;
        this.appId = appId;
        this.notifyUrl = notifyUrl;
        this.key = key;
        this.scene_info = scene_info;
        this.trade_type = trade_type;
    }
	/**
     * 调用统一下单，获得下单结果
     * @return 统一下单结果
	 * @throws Exception 
     * @失败时抛异常Exception
     */
    public WxPayData GetUnifiedOrderResult(String body, String godds_tag,String order_no,String attach,String spbillCreateIp) throws Exception {
    	
        WxPayData data = new WxPayData();
        data.SetValue("body", body);//产品描述
        data.SetValue("attach", attach);//附带数据
        data.SetValue("out_trade_no", order_no);
        data.SetValue("total_fee", total_fee);
        data.SetValue("scene_info", scene_info);
        SimpleDateFormat dfStart = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        data.SetValue("time_start", dfStart.format(new Date()));
        long currentTime = System.currentTimeMillis();
        currentTime += 30*60*1000;
        Date date=new Date(currentTime);
        SimpleDateFormat dfExpire = new SimpleDateFormat("yyyyMMddHHmmss");
        data.SetValue("time_expire", dfExpire.format(date));
        data.SetValue("goods_tag", godds_tag);//商品标记
        data.SetValue("trade_type", this.trade_type);
        if(StringUtils.equals(trade_type, "JSAPI"))
        {
            data.SetValue("openid", openId);
        }
        data.SetValue("mch_id", mhId);
        data.SetValue("appid", appId);
        data.SetValue("notify_url", notifyUrl);
        data.SetValue("spbill_create_ip", spbillCreateIp);
        WxPayData result = WxPayApi.UnifiedOrder(data, key);
        if(result.GetValue("appid")==null || result.GetValue("prepay_id")==null || result.GetValue("prepay_id").toString() == "") {
            throw new Exception("UnifiedOrder response error!");
        }
        unifiedOrderResult = result;
        return result;
    }
    
    /**
     *  
     * 从统一下单成功返回的数据中获取微信浏览器调起jsapi支付所需的参数，
     * 微信浏览器调起JSAPI时的输入参数格式如下：
     * {
     *   "appId" : "wx2421b1c4370ec43b",     //公众号名称，由商户传入     
     *   "timeStamp":" 1395712654",         //时间戳，自1970年以来的秒数     
     *   "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //随机串     
     *   "package" : "prepay_id=u802345jgfjsdfgsdg888",     
     *   "signType" : "MD5",         //微信签名方式:    
     *   "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 
     * }
     * @return string 微信浏览器调起JSAPI时的输入参数，json格式可以直接做参数用
     * 更详细的说明请参考网页端调起支付API：http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7
     * 
     */
    public WxPayData GetJsApiParameters()
    {
        //Log.Debug(this.GetType().ToString(), "JsApiPay::GetJsApiParam is processing...");

        WxPayData jsApiParam = new WxPayData();
        jsApiParam.SetValue("appId", unifiedOrderResult.GetValue("appid"));
        jsApiParam.SetValue("timeStamp", String.valueOf(System.currentTimeMillis()));
        UUID uuid = UUID.randomUUID();
        jsApiParam.SetValue("nonceStr", uuid.toString().replace("-", ""));
        jsApiParam.SetValue("package", "prepay_id=" + unifiedOrderResult.GetValue("prepay_id"));
        jsApiParam.SetValue("signType", "MD5");
        jsApiParam.SetValue("paySign", WxSignUtils.getSign(jsApiParam.GetData()));

        //Log.Debug(this.GetType().ToString(), "Get jsApiParam : " + parameters);
        return jsApiParam;
    }
}
